//
//  MobileCardIndexViewController.m
//  ELife
//
//  Created by  刘奇男 on 11-5-16.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MobileCardIndexViewController.h"
#import "AlertView.h"
#import "GTMBase64.h"
#import "M13UserDefaults.h"
#import "GetMyData.h"

@implementation MobileCardIndexViewController

@synthesize cost, cardNumber, cardPassword, cardType;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    self.title = NSLocalizedString(@"Charge2Title", @"");
    return self;
}

- (void)dealloc
{
    [cost release];
    [cardType release];
    [cardNumber release];
    [cardPassword release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    app = [ELifeAppDelegate App];
    [self.navigationItem setTitle:@"充值卡充值"];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)confirm:(id)sender
{
    
    /*
     18600035966  123456
     
     /mobile2/recharge?version=shengcai_j2me_240x320_common_common_6002_v2.02_301&uname=%E7%94%9F%E6%89%8D%E6%9C%89%E9%81%93&type=02&content=11039160218641188%7C331827228023023321&value=30
     
     cardType;
     UITextField *cost;
     UITextField *cardNumber;
     UITextField *cardPassword;
     */
    
    if ([cardType.text length]>0 && [cost.text length] && [cardNumber.text length] && [cardPassword.text length]) {
        [cardType resignFirstResponder];
        [cost resignFirstResponder];
        [cardNumber resignFirstResponder];
        [cardPassword resignFirstResponder];
        
        [app showProgressDialog:@"正在充值,请稍候"];
        
        NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"%@mobile2/recharge?", [[NSBundle mainBundle] localizedStringForKey:(@"BussinessHost") value:@"" table:nil]]];
        
        NSMutableDictionary *param = [[NSMutableDictionary alloc]initWithCapacity:5];
        
        
//        NSArray* info = [M13UserDefaults getUserDefaultsArray:USERINFO_NAMEANDPASSWORD];
//        NSLog(@"UserInfo:---%@\n",info);
//        
//        NSData* namedata = [[NSString stringWithString:@"13701123260"] dataUsingEncoding:NSUTF8StringEncoding];
//        NSMutableString* retStr = [[NSMutableString alloc]  initWithData:[GTMBase64 encodeData:namedata] encoding:NSUTF8StringEncoding];
        
        [param setObject:[GetMyData getUserId] forKey:@"uname"];
//        [retStr release];
        
        [param setObject:[NSString stringWithFormat:@"0%i", cardType.tag] forKey:@"type"];
        
        NSString* content = [NSString stringWithFormat:@"%@|%@", @"11839198739032874", @"353395814791129219"];
        [param setObject:content forKey:@"content"];
        [param setObject:cost.text forKey:@"value"];
        [param setObject:[[NSBundle mainBundle] localizedStringForKey:(@"ClientVersion") value:@"" table:nil] forKey:@"version"];
        
        
        NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:url, @"iRequestFinished:", nil] forKeys:[NSArray arrayWithObjects:@"url", @"requestMethod", nil]];
        
        [[app httpRequestPoint]openUrl:dic postParam:param target:self];
        
        [param release];
        
    }else
    {
        [AlertView showAlert:@"请填写完整"];
    }
}

-(IBAction)cancel:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)choseCardType:(id)sender
{
    
	UIActionSheet *styleAlert = [[UIActionSheet alloc] initWithTitle:@"充值卡类型:"
                                                            delegate:self cancelButtonTitle:@"取消"
                                              destructiveButtonTitle:nil
                                                   otherButtonTitles:	@"神州行充值卡",
                                 @"电信充值卡",
                                 @"联通充值卡",
                                 nil,
                                 nil];
	
	// use the same style as the nav bar
	styleAlert.actionSheetStyle = self.navigationController.navigationBar.barStyle;
	
	[styleAlert showInView:self.view];
	[styleAlert release];

}

#pragma mark -
#pragma mark UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)modalView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // Change the navigation bar style, also make the status bar match with it
	switch (buttonIndex)
	{
		case 0:
		{
            
            [cardType setText:@"神州行充值卡"];
            cardType.tag = 02;
			break;
		}
		case 1:
		{
            [cardType setText:@"电信充值卡"];
            cardType.tag = 03;
			break;
		}
		case 2:
		{
            [cardType setText:@"联通充值卡"];
            cardType.tag = 04;
			break;
		}
	}
}


#pragma mark text回调
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
	return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
	[textField resignFirstResponder];
	return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
	[textField resignFirstResponder];
	return YES;
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
	[textView resignFirstResponder];
	return YES;
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    return YES;
}


#pragma mark -
#pragma mark 联网回调
- (void)iRequestFinished:(NSData *)request
{
    [app hideProgressDialog];
    
    NSMutableString* retStr = [[NSMutableString alloc]  initWithData:request encoding:NSUTF8StringEncoding];
    NSLog(@"----------------%@", retStr);
    
    if ([retStr isEqualToString:@"01"]) {
        [AlertView showAlert:@"充值成功"];
    }
    
    [retStr release];
}



- (IBAction)closeKeypad:(id)sender
{
    [cardPassword resignFirstResponder];
    [cardNumber resignFirstResponder];
    [cost resignFirstResponder];
    
    [sender resignFirstResponder];
}


@end
